package com.pactera.asmp.server.session;

import com.pactera.asmp.server.filter.KickoutSessionFilter;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ProjectName: asmp-server
 * @Package com.pactera.asmp.server.session.interceptor
 * @ClassName: MySessionListener
 * @Description: MySessionListener
 * @Date 2020/08/01 15:23
 * @Author lvzhiqiang
 * @Version 1.0
 */
@Component
public class ShiroSessionListener implements SessionListener {
    private static final Logger logger = LoggerFactory.getLogger(ShiroSessionListener.class);
    private final AtomicInteger sessionCount = new AtomicInteger(0);

    @Override
    public void onStart(Session session) {
        sessionCount.incrementAndGet();
        logger.info("ShiroSessionListener onStart session.id:" + session.getId()
                + " session.host:"+ session.getHost()+ " session.timeout:"+ session.getTimeout() + " SessionCount: "+getSessionCount());
    }

    @Override
    public void onStop(Session session) {
        sessionCount.decrementAndGet();
        logger.info("ShiroSessionListener onStop session.id:" + session.getId()
                + " session.host:"+ session.getHost()+ " session.timeout:"+ session.getTimeout() + " SessionCount: "+getSessionCount());
    }

    @Override
    public void onExpiration(Session session) {
        sessionCount.decrementAndGet();
        logger.info("ShiroSessionListener onExpiration session.id:" + session.getId()
                + " session.host:"+ session.getHost()+ " session.timeout:"+ session.getTimeout() + " SessionCount: "+getSessionCount());
    }

    public int getSessionCount() {
        return sessionCount.get();
    }


}
